package com.sca.common.excel.module.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.sca.common.excel.module.domain.Member;
import com.sca.common.excel.module.helper.ExcelAnalysisHelper;
import com.sca.common.excel.module.listener.ExcelListener;
//import com.sca.core.common.domain.ResponseDTO;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Author: denglei
 * @Date: 2022/3/15 23:51
 * @Description:
 */
@Controller
@RequestMapping("/easyExcel")
public class EasyExcelController {

    @SneakyThrows(IOException.class)
    @RequestMapping(value = "/exportMemberList", method = RequestMethod.GET)
    @ResponseBody
    public void exportMemberList(HttpServletResponse response) {
        setExcelRespProp(response, "会员列表");
        List<Member> memberList = new ArrayList<>();
        Member member=new Member();
        member.setBirthday(new Date());
        member.setGender(0);
        member.setIcon("22");
        member.setId(1L);
        member.setNickname("用户1");
        member.setPassword("2333");
        member.setPhone("15522222222");
        member.setUsername("admin");
        memberList.add(member);

        EasyExcel.write(response.getOutputStream())
                .head(Member.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("会员列表")
                .doWrite(memberList);
    }

    /**
     * 设置excel下载响应头属性
     */
    private void setExcelRespProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setContentType("multipart/form-data");

        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }



    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public  String trs(){
        return "111";
    }

    @PostMapping("/importExcel")
    @ResponseBody
    public Map<String,Object> importExcel(@RequestParam(value="file", required = false) MultipartFile file, String employeeId){
        Map<String,Object>map=new HashMap<>();
        map.put("data",null);
        map.put("code",100);



        if ( !(file.getOriginalFilename().toLowerCase().endsWith("xlsx") || file.getOriginalFilename().toLowerCase().endsWith("xls"))){
            map.put("code",100);
            map.put("message","文件格式不正确,请选择xlsx,xls的文件！");
            return  map;
        }
        InputStream is=null;
        try {
            is=file.getInputStream();
            EasyExcel.read(is,new ExcelListener()).sheet().doRead();
//            new ExcelAnalysisHelper().getList();

        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("code",200);
        map.put("message","成功");
        return  map;
    }


}
